/* THIS FILE WAS AUTOGENERATED BY PANWRAP. FEEL FREE TO EDIT THOUGH, HON <3 */

#define _LARGEFILE64_SOURCE 1
#define CACHE_LINE_SIZE 1024 /* TODO */ 

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "pandev.h"

void main(void) {
  int fd = pandev_raw_open(), rc;
  
  if (fd < 0) {
    printf("Error opening kernel\n");
  }
  
  struct mali_ioctl_get_version get_version_0 = {
    .major =  10,
    .minor =   6,
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_GET_VERSION, &get_version_0);
  if (rc) printf("Error %d in GET_VERSION_0\n", rc);

  pandev_map_mtp(fd);
  
  struct mali_ioctl_set_flags set_flags_1 = {
    .create_flags = 00000000,
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_SET_FLAGS, &set_flags_1);
  if (rc) printf("Error %d in SET_FLAGS_1\n", rc);

  struct mali_ioctl_mem_alloc mem_alloc_4 = {
    .va_pages = 32,
    .commit_pages = 32,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD,
    // GPU memory allocated at GPU VA 0x102000000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_4);
  if (rc) printf("Error %d in MEM_ALLOC_4\n", rc);

  struct mali_ioctl_mem_alloc mem_alloc_5 = {
    .va_pages = 1,
    .commit_pages = 1,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR | MALI_MEM_SAME_VA,
    // GPU memory allocated at GPU VA 0x41000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_5);
  if (rc) printf("Error %d in MEM_ALLOC_5\n", rc);

  uint32_t *memory_5 = mmap64(NULL, 4096, 3, 1, fd, mem_alloc_5.gpu_va);

  if (memory_5 == MAP_FAILED) printf("Error mapping memory_5\n");

  struct mali_ioctl_mem_alloc mem_alloc_6 = {
    .va_pages = 1,
    .commit_pages = 1,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD,
    // GPU memory allocated at GPU VA 0x102020000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_6);
  if (rc) printf("Error %d in MEM_ALLOC_6\n", rc);

  uint32_t *memory_6 = mmap64(NULL, 4096, 2, 1, fd, mem_alloc_6.gpu_va);

  if (memory_6 == MAP_FAILED) printf("Error mapping memory_6\n");

  struct mali_ioctl_mem_alloc mem_alloc_8 = {
    .va_pages = 64,
    .commit_pages = 64,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR //| MALI_MEM_CACHED_CPU,
    // GPU memory allocated at GPU VA 0x102021000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_8);
  if (rc) printf("Error %d in MEM_ALLOC_8\n", rc);

  struct mali_ioctl_mem_alloc mem_alloc_9 = {
    .va_pages = 64,
    .commit_pages = 64,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR | MALI_MEM_SAME_VA,
    // GPU memory allocated at GPU VA 0x41000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_9);
  if (rc) printf("Error %d in MEM_ALLOC_9\n", rc);

  uint32_t *memory_9 = mmap64(NULL, 262144, 3, 1, fd, mem_alloc_9.gpu_va);

  if (memory_9 == MAP_FAILED) printf("Error mapping memory_9\n");

  struct mali_ioctl_mem_alloc mem_alloc_11 = {
    .va_pages = 8,
    .commit_pages = 8,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR,
    // GPU memory allocated at GPU VA 0x102061000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_11);
  if (rc) printf("Error %d in MEM_ALLOC_11\n", rc);

  struct mali_ioctl_mem_alloc mem_alloc_12 = {
    .va_pages = 4096,
    .commit_pages = 1,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_EX,
    // GPU memory allocated at GPU VA 0x101000000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_12);
  if (rc) printf("Error %d in MEM_ALLOC_12\n", rc);

  uint32_t *shader_12 = mmap64(NULL, 4096, 3, 1, fd, mem_alloc_12.gpu_va);

  if (shader_12 == MAP_FAILED) printf("Error mapping shader_12\n");

  struct mali_ioctl_mem_alloc mem_alloc_14 = {
    .va_pages = 1,
    .commit_pages = 1,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR,
    // GPU memory allocated at GPU VA 0x102069000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_14);
  if (rc) printf("Error %d in MEM_ALLOC_14\n", rc);

  uint32_t *framebuffer;
  posix_memalign((void **) &framebuffer, CACHE_LINE_SIZE, 4096*4096*4);
  struct mali_mem_import_user_buffer framebuffer_handle = { .ptr = (uint64_t) (uintptr_t) framebuffer, .length = 4096*4096*4 };
  struct mali_ioctl_mem_import mem_import_15 = {
    .phandle = (uint64_t) (uintptr_t) &framebuffer_handle,
    .type = MALI_MEM_IMPORT_TYPE_USER_BUFFER,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR,
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_IMPORT, &mem_import_15);
  if (rc) printf("Error %d in MEM_IMPORT_15\n", rc);

  uint64_t framebuffer_va = mem_import_15.gpu_va;
  struct mali_ioctl_mem_alloc mem_alloc_17 = {
    .va_pages = 95,
    .commit_pages = 95,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR,
    // GPU memory allocated at GPU VA 0x1020c8000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_17);
  if (rc) printf("Error %d in MEM_ALLOC_17\n", rc);

  struct mali_ioctl_mem_alloc mem_alloc_18 = {
    .va_pages = 8,
    .commit_pages = 8,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR /*| MALI_MEM_CACHED_CPU */| MALI_MEM_SAME_VA,
    // GPU memory allocated at GPU VA 0x41000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_18);
  if (rc) printf("Error %d in MEM_ALLOC_18\n", rc);

  uint32_t *memory_18 = mmap64(NULL, 32768, 3, 1, fd, mem_alloc_18.gpu_va);

  if (memory_18 == MAP_FAILED) printf("Error mapping memory_18\n");

  struct mali_ioctl_mem_alloc mem_alloc_19 = {
    .va_pages = 8,
    .commit_pages = 8,
    .extent = 0x0,
    .flags = MALI_MEM_PROT_CPU_RD | MALI_MEM_PROT_CPU_WR | MALI_MEM_PROT_GPU_RD | MALI_MEM_PROT_GPU_WR | MALI_MEM_SAME_VA,
    // GPU memory allocated at GPU VA 0x41000
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_MEM_ALLOC, &mem_alloc_19);
  if (rc) printf("Error %d in MEM_ALLOC_19\n", rc);

  uint32_t *memory_19 = mmap64(NULL, 32768, 3, 1, fd, mem_alloc_19.gpu_va);

  if (memory_19 == MAP_FAILED) printf("Error mapping memory_19\n");

  struct mali_job_descriptor_header job_0 = {
    .job_type = 9,
    .job_descriptor_size = 0,
    .exception_status = 0,
    .first_incomplete_task = 0,
    .fault_pointer = 0x0,
    .job_barrier = 0,
    .job_index = 1,
    .job_dependency_index_1 = 0,
    .job_dependency_index_1 = 0,
    .next_job = 0x0,
  };
  memcpy(memory_18 + 0, &job_0, sizeof(job_0));
  struct mali_payload_fragment fragment_0 = {
    ._min_tile_coord = MALI_COORDINATE_TO_TILE_MIN(0, 0, 0),
    ._max_tile_coord = MALI_COORDINATE_TO_TILE_MAX(400, 240, 0),
    .fbd = mem_alloc_19.gpu_va + (8064*4) | MALI_SFBD,
  };
  memcpy(memory_18 + 8, &fragment_0, sizeof(fragment_0));
  struct mali_tentative_sfbd fbd_0 = {
    .unknown1 = 0x0,
    .flags = 0x1f,
    .heap_free_address = 0x0,
    .unknown2 = 0xb84e0281,
    .unknown3 = 0x141100,
    .unknown4 = 0xef018f,
    .weights = { 0x0, 0x0, 0x0, 0x0, 0x20c75c0, 0x1, 0xfffff9c0, 0x0, },
    .pdcm_1_1 = 0x1020c8000,
    .pdcm_1_2 = 0x6400,
    .pdcm_2_1 = 0x1020c8000,
    .pdcm_2_2 = 0x6400,
    .clear_color_1 = 0x664c331a,
    .clear_color_2 = 0x664c331a,
    .clear_color_3 = 0x664c331a,
    .clear_color_4 = 0x664c331a,
    .clear_depth_1 = 0.750000,
    .clear_depth_2 = 0.750000,
    .clear_depth_3 = 0.750000,
    .clear_depth_4 = 0.750000,
    .clear_stencil = 0xf1,
    .unknown_address_1 = mem_alloc_5.gpu_va + 0,
    .unknown_address_2 = mem_alloc_5.gpu_va + 512,
    .shader_1 = 0x102061000,
    .unknown8 = 0x200,
    .unknown9 = 0x1000,
    .shader_3 = 0x102000000,
    .shader_4 = 0x102000000,
  };
  memcpy(memory_19 + 8064, &fbd_0, sizeof(fbd_0));

  mali_external_resource resources_0_0[] = {
    framebuffer_va | MALI_EXT_RES_ACCESS_EXCLUSIVE,
  };

  struct mali_jd_atom_v2 atoms_0[] = {
    {
      .jc = mem_alloc_18.gpu_va + 0,
      .nr_ext_res = 1,
      .ext_res_list = resources_0_0,
      .pre_dep = {
      },
      .atom_number = 1,
      .prio = 0,
      .device_nr = 0,
      .compat_core_req = MALI_JD_REQ_FS | MALI_JD_REQ_EXTERNAL_RESOURCES
    },
  };

  struct mali_ioctl_job_submit job_submit_21 = {
    .addr = atoms_0,
    .nr_atoms = 1,
    .stride = 48,
  };

  rc = pandev_ioctl(fd, MALI_IOCTL_JOB_SUBMIT, &job_submit_21);
  if (rc) printf("Error %d in JOB_SUBMIT_21\n", rc);

  FILE *fp = fopen("/dev/shm/framebuffer.bin", "wb");
  fwrite(framebuffer, 1, 4096*4096*4, fp);
  fclose(fp);
}
